//
//  NetworkService 2.h
//  MiniWeChat
//
//  Created by ByteDance on 2025/6/26.
//


#import "NetworkService.h"
#import "AFNetworking.h"

@interface NetworkService ()

@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;

@end

@implementation NetworkService

+ (instancetype)sharedInstance {
    static NetworkService *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.example.com"]];
        _sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        _sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        // 初始化长连接或轮询
        [self setupMessageListener];
    }
    return self;
}

- (void)setupMessageListener {
    // 实际项目中这里会实现 WebSocket 或长连接
    // 简化示例，使用定时器模拟消息接收
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        // 模拟接收到消息
        // 实际项目中会从服务器接收消息并分发
    }];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)sendMessage:(MessageModel *)message completion:(MessageSendCompletion)completion {
    NSDictionary *parameters = @{
        @"conversation_id": message.conversation_id,
        @"sender": message.sender,
        @"type": @(message.type),
        @"content": message.content,
        @"time": @(message.time)
    };
}

- (void)fetchUserById:(NSString *)uid completion:(DataCompletion)completion {
}

@end
